/**
 * @filename:FlowTypeServiceImpl 2023-5-10
 * @project Wonder Vue Admin Api  1.1.0
 * Copyright(c) 2022 Wonder Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.app.service.impl;

import com.wonder.app.domain.FlowType;
import com.wonder.app.mapper.FlowTypeDao;
import com.wonder.app.domain.LanguageField;
import com.wonder.app.service.LanguageFieldService;
import com.wonder.app.service.FlowTypeService;
import com.wonder.app.request.FlowTypeRequest;
import com.wonder.common.utils.StringUtils;
import com.wonder.common.utils.MessageUtils;
import com.wonder.common.common.AjaxResult;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.lang.reflect.Method;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**   
 * @Description:TODO(流水类型服务实现)
 *
 * @version: 1.1.0
 * @author: Wonder
 * 
 */
@Service
public class FlowTypeServiceImpl  extends ServiceImpl<FlowTypeDao, FlowType> implements FlowTypeService  {

	@Autowired
	LanguageFieldService languageFieldService;    
    /**
     * 验证流水类型信息
     * 
     * @param flowType
     * @param type
     * @return String
     */
    @Override
    public String validate(FlowType flowType,String type){
    
    
       return "";
    }
    
     /**
	 * Set the property by the name
	 * @param flowType
	 * @param method
	 * @param value
	 * @throws Exception
	 */
	private void SetLan(Object flowType,String method,String value) throws Exception{
		    Method method1 = flowType.getClass().getMethod(method,String.class);
		    method1.invoke(flowType,value);	

	}
	
	/**
	 * 根据语言获取该语言的数据
	 * @param id
	 * @param language
	 * @return
	 */
	@Override
	public FlowType getInfo(Long id,String language) {
		FlowType flowType=this.getById(id);
		if(flowType==null) return flowType;
		if(StringUtils.hasText(language)) {
			
				List<LanguageField> fields=languageFieldService.list(new QueryWrapper<LanguageField>()
						.eq("table_key", flowType.getId()).eq("table_name", "sn_flow_type").eq("code", language));
				for (LanguageField languageField : fields) {
					String columnName=languageField.getTable_column();
					if(languageField.getContent()==null) continue;//Not set for null value
					try {
						SetLan(flowType, "set"+StringUtils.firstCharToUppercase(columnName), languageField.getContent());
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
			
		}		
		
		return flowType;
		
	}
	
	/**
	 * Description:获取全部商家
	 * @param language
	 */
	@Override 
	public List<FlowType> list(String language){
	    List<FlowType> list=this.list();	
        if(StringUtils.hasText(language)) {
        	language=language.replace("-", "_");
			for(FlowType  flowType : list) {
				List<LanguageField> fields=languageFieldService.list(new QueryWrapper<LanguageField>()
						.eq("table_key", flowType.getId()).eq("table_name", "sn_flow_type").eq("code", language));
				for (LanguageField languageField : fields) {
					String columnName=languageField.getTable_column();
					if(languageField.getContent()==null) continue;
					try {
						SetLan(flowType, "set"+StringUtils.firstCharToUppercase(columnName), languageField.getContent());
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
			}
		}
		return list;
    } 	
	   
    
    /**
	 * Description:流水类型分页返回
	 * @param request //检索条件
	 * @param page //分页参数
	 * @return IPage<FlowType>   //分页记录
	 */
	@Override 
	public IPage<FlowType> selectList(FlowTypeRequest request, Page<FlowType> page){
	    LambdaQueryWrapper<FlowType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
	    
	    if (null != request.getId()) {
	       lambdaQueryWrapper.eq(FlowType::getId, request.getId());
	      }               
             
        
		lambdaQueryWrapper.orderByDesc(FlowType::getId);	
		Page<FlowType> selectPage = this.baseMapper.selectPage(page, lambdaQueryWrapper);
        if(StringUtils.hasText(request.getLanguage())) {
			for(FlowType  flowType : selectPage.getRecords()) {
				List<LanguageField> fields=languageFieldService.list(new QueryWrapper<LanguageField>()
						.eq("table_key", flowType.getId()).eq("table_name", "sn_flow_type").eq("code", request.getLanguage()));
				for (LanguageField languageField : fields) {
					String columnName=languageField.getTable_column();
					if(languageField.getContent()==null) continue;
					try {
						SetLan(flowType, "set"+StringUtils.firstCharToUppercase(columnName), languageField.getContent());
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
			}
		}
		return selectPage;
    }
        
    
	
}